<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/rear-end/index' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>商品管理</el-breadcrumb-item>
  <el-breadcrumb-item>商品列表</el-breadcrumb-item>
</el-breadcrumb>
<!-- 卡片视图区 -->
<el-card>
    <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入商品名" v-model="queryInfo.query" clearable @clear="getGoodsList">
          <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
        <el-button type="primary" @click="goAddpage">添加商品</el-button></el-col>
    </el-row>
    <!-- table表格区域 -->
    <el-table :data="goodslist" :border="true" stripe>
    <el-table-column type="index"></el-table-column>
    <el-table-column label="商品名称" prop="goodsName"></el-table-column> 
    <el-table-column label="商品价格（元）" prop="goodsPrice" width="95px"></el-table-column> 
    <el-table-column label="商品库存" prop="goodsNumber" width="70px"></el-table-column> 
    <el-table-column label="创建时间" width="140px">
      <template slot-scope="scope">
        {{scope.row.createTime|dateFormat}}
      </template>
    </el-table-column> 
    <el-table-column label="操作" width="130px">
      <template slot-scope="scope">
        <el-button type="primary" @click="showEditDialog(scope.row)" icon="el-icon-edit" size="mini"></el-button>
        <el-button type="danger" @click="removeById(scope.row.goodsId)" icon="el-icon-delete" size="mini"></el-button>
      </template>
    </el-table-column> 
  </el-table>
      <!-- 分页 -->
      <el-pagination
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="queryInfo.pagenum"
:page-sizes="[ 10, 15, 20,30]"
:page-size="queryInfo.pagesize"
layout="total, sizes, prev, pager, next, jumper"
:total="total" background>
</el-pagination>
</el-card>
<el-dialog
  title="修改商品信息"
  :visible.sync="dialogVisible"
  width="50%"
  :before-close="handleClose"
  >
  <UpdateVue ref="updateRef"></UpdateVue>
  <span slot="footer" class="dialog-footer">
    <el-button @click="handleClose">取 消</el-button>
    <el-button type="primary" @click="update">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
import UpdateVue from '@/components/rear-end/goods/Update.vue'
import _ from 'lodash'

export default {
data(){return{
//查询参数对象
queryInfo:{
  query:'',
  pagenum:1,
  pagesize:10
},
//商品列表
goodslist:[],
//总数
total:0,
//打开修改对话框
dialogVisible:false,
}},
components:{UpdateVue},
created(){
  this.getGoodsList()
},
methods:{
 async getGoodsList(){
   const res=await  this.$api.rear_end.goods(this.queryInfo)
  if(res.data.code!==1){
    this.$message.error(res.data.msg)
  }
this.goodslist=res.data.data
this.total=res.data.map.total-0
  },
  //修改分页每条多少数量
  handleSizeChange(val) {
   this.queryInfo.pagesize=val
  const maxnum=Math.ceil(this.total/val)
    if(this.queryInfo.pagenum>maxnum){
      this.queryInfo.pagenum=maxnum
    }    
        this.getGoodsList()
      },
      //修改当前页码
  handleCurrentChange(val) {
    this.queryInfo.pagenum=val
    this.getGoodsList()
  },
  //根据id删除商品
  removeById(id){
    this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
        const res=await  this.$api.rear_end.delGoods({goodsId:id})
          if(res.data.code===1){
            if(this.total<this.queryInfo.pagesize*this.queryInfo.pagenum&&this.queryInfo.pagenum>1){
           this.queryInfo.pagenum=this.queryInfo.pagenum-1
          } 
           this.getGoodsList()
            this.$message({
            type: 'success',
            message: '删除成功!'
          })
          }
          else{
            this.$message.error(res.data.msg)
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
  },
  //打开修改对话框
  showEditDialog(row){
    this.dialogVisible=true
    this.$nextTick(()=>{
      // updateForm=row
      const form= _.cloneDeep(row)
     this.$refs.updateRef.updateForm1(form)
    })
  },
  //关闭对话框
  handleClose(){
    this.$refs.updateRef.clearUpdateForm()
    this.dialogVisible=false
  },
  //修改商品函数
  update(){
    this.$refs.updateRef.update()
    this.getGoodsList()
    this.dialogVisible=false
    
  },
  //跳转添加商品
  goAddpage(){
this.$router.push('/goods/add')
  }
  
}
}
</script>

<style>

</style>